<script>
	import {
		login,
		getisOnline,
		ttlogin,
		getOfficialAppID
	} from '@/module/index.js'
	import {
		config
	} from '@/config/index.js'
	export default {
		globalData: {
			iosFlag: true,
			LinkType: null
		},
		onLaunch: function(options) {
			let {
				official_account_id,
				uid,
				video_id,
				link_id,
				video_detail_id,
				ip,
				ua
			} = options.query; //公众号参数
			// 保存各种落地页传过来的参数
			uni.setStorageSync('login_query', {
				query:options.query,
				path:options.path,
			})
			// 如果有token
			if (uni.getStorageSync('token')) {
				this.$isResolve();
				return
			}
			// h5登录  
			if (this.isWechat()) {
				this.getCode(official_account_id, link_id)
				return
			}
			// 小程序登录
			let that = this
			tt.login({
				success(data) {
					let query = {
						app_id: tt.getEnvInfoSync().microapp.appId,
						code: data.code,
						...options.query
						}
					ttlogin(query).then((res) => {
						if (res.code === 200) {
							uni.setStorageSync('token', res.data.token)
							uni.setStorageSync('login', res.data)
							uni.setStorageSync('is_online', (res.data.is_online_a == 1 ? false :
								true) ?? false)
							uni.setStorageSync('environment', 'applet') // 当前项目运行环境  小程序
						} else {
							uni.showToast({
								title: res.message,
								icon: "none"
							})
						}
						that.$isResolve();
					}).catch(err => {
						console.log(err)
						that.$isResolve();
					})
				}
			})

			this.globalData.LinkType = uni.getStorageSync('is_online')
			var self = this
			uni.getSystemInfo({
				success: res => {
					if (res.osName === 'ios') {
						self.globalData.iosFlag = true
					} else {
						self.globalData.iosFlag = true
					}
				}
			})
		},
		methods: {
			isWechat() {
				return String(navigator?.userAgent?.toLowerCase()?.match(/MicroMessenger/i)) === "micromessenger";
			},
			getCode(id, link_id) {
				// 非静默授权，第一次有弹框
				let code = '';
				let callback_url = config.callback_url; // 获取页面url
				let state = this.getUrlCode().state || '';
				code = this.getUrlCode().code; // 截取code
				if (link_id) {
					id = `${id}_${link_id}`
				}
				if (code == null || code === '') {
					// 如果没有code，则去请求
					uni.showLoading({
						title: '加载中'
					});
					let sid = id.split('_')
					sid = sid[0] || ''
					getOfficialAppID({official_account_id:sid}).then((res) => {
						if (res.code == 200) {
							let appid = res.data.app_id
							uni.hideLoading();
							window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
						    callback_url
						)}&response_type=code&scope=snsapi_userinfo&state=${id}#wechat_redirect`;
						} else {
							uni.hideLoading();
							uni.showToast({
								title: res.message,
								icon: "none"
							})
						}
					}).catch((err) => {
						uni.hideLoading();
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					})
				} else {
					// 当code不等于空时，调用后端接口获取用户信息
					this.getUserInfo(code, state);
				}
			},
			// 从url中获取code返回
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				// this.winUrl = url;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				}
				return theRequest;
			},
			getUserInfo(code, uid) {
				let data = {
					code
				}
				uid = uid.split('_')
				data.official_account_id = uid[0] || ''
				if (uid.length > 1) {
					data.link_id = uid[1]
				}
				ttlogin(data).then((res) => {
					if (res.code == 200) {
						uni.setStorageSync('token', res.data.token)
						uni.setStorageSync('login', res.data)
						uni.setStorageSync('is_online', true) // h5下默认开启
						uni.setStorageSync('environment', 'official_account') // 当前项目运行环境  公众号网页
						this.globalData.LinkType = true
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
					this.$isResolve();
				}).catch((err) => {
					this.$isResolve();
					console.log(err)
				})
			}
		}
	}
</script>

<style>
	@import "/wxcomponents/vant/dist/common/index.wxss";
	@import "/iconfont/index.css";
	@import "/until/tt-vant-index.css";
	/*每个页面公共css */
	page {
		background-color: #ffffff;
		font-family: '微软雅黑 Light';
	}
</style>